#include "Tree.h"
#include "Queue.h"

// 队列的初始化

void QInit (Queue* pq)
{

    assert (pq) ;

    pq -> ptail = pq -> phead = NULL ;

}

// 队列的销毁

void QDestory (Queue* pq)
{

    assert (pq) ;

    QNode* pcur = pq -> phead ;

    while (pcur)
    {

        QNode* next = pcur -> next ;

        free (pcur) ;

        pcur = next ;
        
    }

    pq -> phead = pq -> ptail = NULL ;

}

// 入队---队尾

void QPush (Queue* pq , QDataType* x)
{

    QNode* newnode = (QNode*) malloc (sizeof (QNode)) ;

    if (newnode == NULL)
    {

        perror ("malloc fail!") ;

        exit (1) ;

    }

    newnode -> data = x ;

    newnode -> next = NULL ;

    if (pq -> phead == NULL)
    {

        pq -> phead = pq -> ptail = newnode ;

    }

    else
    {
        
        pq -> ptail -> next = newnode ;

        pq -> ptail = pq -> ptail -> next ;

    }

}

// 出队---队头

void QPop (Queue* pq)
{

    assert (!QEmpty (pq)) ;

    if (pq -> phead == pq -> ptail)
    {

        free (pq -> phead) ;

        pq -> phead = pq -> ptail = NULL ;

    }

    else
    {

        QNode* next = pq -> phead -> next ;

        free (pq -> phead) ;

        pq -> phead = next ;

    }

}

// 队列判空

bool QEmpty (Queue* pq)
{

    assert (pq) ;

    return pq -> phead == NULL ;

}

// 取队头数据

QDataType* QFront (Queue* pq)
{

    assert (!QEmpty (pq)) ;

    return pq -> phead -> data ;

}